<?php

namespace app\common\model;

use think\Db;
use think\Model;

class Order extends Base
{
    protected $redata;

    public function _initialize()
    {
        $this->redata = ['content' => '', "status" => 0, 'msg' => ''];
    }


    /**
     * 快捷方法 返回出错信息
     */
    protected function err($msg)
    {
        $this->redata['msg'] = $msg;
        $this->redata['status'] = 0;
        return $this->redata;
    }

    /**
     * 快捷方法 返回成功
     */
    protected function suc($data = '')
    {
        $this->redata['content'] = $data;
        $this->redata['status'] = 1;
        return $this->redata;
    }

    /**
     *创建订单
     * $orderSn  订单编号
     * $type     订单类型
     * $employeeId  雇主主键
     * $workId      阿姨主键
     * $orderAmount  订单金额
     * $userCouponId  优惠券主键 可以为空
     */
    public function addOrder($orderSn=0,$type=0,$employeeId=0,$workId=0,$orderAmount=0,$userCouponId=0,$shareId=0){

        //判断参数合法性
        if(empty($orderSn)){
            return  $this->err('缺少订单编号！');
        }
        if(empty($type)){
            return $this->err('缺少订单类型！');
        }
        if(empty($employeeId)&&empty($workId)){
            return $this->err('缺少用户主键！');
        }
        if(empty($orderAmount)){
            return $this->err('缺少订单价格！');
        }
        if (empty($employeeId) && !empty($workId)) {
            $id = $workId;
        }
        if (!empty($employeeId) && empty($workId)) {
            $id = $employeeId;
        }
        if (!empty($employeeId) && !empty($workId)) {
            $id = $employeeId;
        }
        $user=model('user')->field('province,city,district,street')->where(['id'=>$id])->find();

        if($shareId){
            //创建订单数据
            $data = [
                'orderSn' => $orderSn,
                'shareId'=>$shareId,
                'type' => $type,
                'employeeId' => $employeeId,
                'workId' => $workId,
                'orderAmount' => $orderAmount,
                'totalAmount' => $orderAmount,
                'createTime' => createTime(),
                'province' => $user['province'],
                'city' => $user['city'],
                'district' => $user['district'],
                'address' => $user['street']
            ];
        }else{
            //创建订单数据
            $data = [
                'orderSn' => $orderSn,
                'type' => $type,
                'employeeId' => $employeeId,
                'workId' => $workId,
                'orderAmount' => $orderAmount,
                'totalAmount' => $orderAmount,
                'createTime' => createTime(),
                'province' => $user['province'],
                'city' => $user['city'],
                'district' => $user['district'],
                'address' => $user['street']
            ];
        }

        $res=$this->editData($data);

        if(!$res){
            return  $this->err('创建订单失败！');
        }
        else{
            return  $this->suc($res);
        }

    }
    /**
     *    支付成功后根据订单类型 修改相关数据
     *    1-雇佣阿姨 2-雇主购买月卡 3-阿姨购买月卡 4-好雇主认证 5-好阿姨认证
     */

    public function updateStatus($employeeId = 0, $workId = 0, $type = 0)
    {

        //判断参数合法
        if (empty($type)) {
            return $this->err('订单类型！');
        }
        if (empty($employeeId) && empty($workId)) {
            return $this->err('缺少用户主键！');
        }
        $createTime = time();
      //  $vipTime = 30 * 86400;
        $vipTime = 3 * 86400;
        $vip = [
            'vipEndTime' => date('Y-m-d H:i:s', $createTime + $vipTime)
        ];

        //年卡
        $yearVipTime = 365 * 86400;
        $yearVip = [
            'vipEndTime' => date('Y-m-d H:i:s', $createTime + $yearVipTime)
        ];


        switch ($type) {
            case 1 :
              /*  //一键雇佣
                $Hire = [
                    'employeeId' => $employeeId,
                    'workId' => $workId,
                    'isHire' => 1,
                    'hireTime' => createTime(),
                    'createTime' => createTime()
                ];*/
                $res = model('user_relation')->where(['employeeId'=>$employeeId,'workId'=>$workId])->update(['isHire'=>1]);
                break;
            case 2:
                //雇主购买月卡
                $vip['id'] = $employeeId;
                $res = model('user')->editData($vip, $vip['id']);
                break;
            case 3:
                //阿姨购买月卡
                $vip['id'] = $workId;
                $res = model('user')->editData($vip, $vip['id']);
                break;
            case 4:
                // 好雇主认证
                $GoodEmployer = [
                    'id' => $employeeId,
                    'isGoodEmployer' => 1
                ];
                $res = model('user')->editData($GoodEmployer, $GoodEmployer['id']);
                break;
            case 5:
                // 好阿姨认证
                $GoodWorker = [
                    'id' => $workId,
                    'isGoodWorker' => 1
                ];
                $res = model('user')->editData($GoodWorker, $GoodWorker['id']);
                break;
            case 8:

                //雇主购买年卡
                $yearVip['id'] = $employeeId;
                $res = model('user')->editData($yearVip, $yearVip['id']);

                break;

            case 9:

                //阿姨购买月卡
                $yearVip['id'] = $workId;
                $res = model('user')->editData($yearVip, $yearVip['id']);

                break;
            default :
                return $this->err('订单类型错误！');
                break;

        }
        if (!$res) {
            return $this->err('用户状态修改失败！');
        } else {
            return $this->suc($res);
        }
    }

}
